#!/bin/bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -e

if command -v flatpak-spawn >/dev/null; then
    SPAWN=(flatpak-spawn --host)
else
    SPAWN=()
fi

MKOSI_CONFIG="$("${SPAWN[@]}" mkosi --json summary | jq -r .Images[-1])"
BUILDDIR="$(jq -r .BuildDirectory <<< "$MKOSI_CONFIG")"
BUILDSUBDIR="$(jq -r .BuildSubdirectory <<< "$MKOSI_CONFIG")"

exec "${SPAWN[@]}" mkosi \
    --rerun-build-scripts \
    build \
    -- \
    clangd \
    --compile-commands-dir=/work/build \
    --path-mappings="\
$(pwd)=/work/src,\
$BUILDDIR/$BUILDSUBDIR=/work/build"\
    "$@"
